﻿using Core;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Data;
using System.Windows.Markup;

namespace HandyWpf.Extensions
{
    /// <summary>
    /// markup基类，使用代理模式获取容器内对应实例，以及保证markup语法不报错
    /// </summary>
    /// <typeparam name="T">代理的类型</typeparam>
    public abstract class AbstractProviderExtension<T>:MarkupExtension
    {
        public Type ServiceType { get; set; }
        protected T instance;
        public override object ProvideValue(IServiceProvider serviceProvider)
        {
            object service = Program.GetService(ServiceType);
            if (service is T TService)
            {
                instance = TService;
            }
            else
            {
                throw new ArgumentException($"ServiceType is not an {typeof(T)}");
            }
            return this;
        }
    }
}
